#pragma once
#include <iostream>
#include <string>
#include <unistd.h>
#include <pthread.h>
#include <functional>
#include <sys/syscall.h> 

#define get_lwp_id() syscall(SYS_gettid)

using func_t = std::function<void()>;


class Thread
{
public:
    Thread(func_t func, std::string name = "None-name")
            : _name(name),
              _func(func),
              _running(false)
    {
        std::cout << "create thread success" << std::endl;
    }

    static void* start_routine(void* args)
    {
        Thread* self = static_cast<Thread*>(args);
        self->_running = true;
        self->_lwpid = get_lwp_id();
        self->_func();//执行对应函数
        pthread_exit((void*)0);//退出

    }

    void Start()
    {
        int n = pthread_create(&_tid, nullptr, start_routine, this);
        if(n == 0)
        {
            std::cout << "run thread success" << std::endl;
        }
    }

    void Join()
    {
        if(_running = false)
            return;

        int m = pthread_join(_tid, nullptr);
         if (m == 0)
        {
            std::cout << "pthread_join success" << std::endl;
        }
 
    }

    ~Thread()
    {}
private:
    pthread_t _tid;
    func_t _func;
    std::string _name;
    pid_t _lwpid;
    bool _running;

};